﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmCauHinhLopDaoTao : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmCauHinhLopDaoTao()
        {
            InitializeComponent();
            this.Load();
            //comboBox1_SelectionChangeCommitted(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {

            msql = "Select * from tb_CTdaotao order by CT_daotao";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            cchuongtrinhdt.DataSource = dt;
            cchuongtrinhdt.DisplayMember = "ct_daotao";
            cchuongtrinhdt.ValueMember = "MS_daotao";

           

           
        }

        private void cchuongtrinhdt_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select a.* from tb_Khoadaotao a " +
                  " inner join tb_cauhinhCtdaotao b " +
                  " on a.MS_Khoadaotao=b.ms_Khoadaotao " +
                  " Where b.ms_daotao="+cchuongtrinhdt.SelectedValue.ToString();
            DataTable dt1 = mLibrary.GetDataTable(msql, "table");
            ckhoa.DataSource = dt1;
            ckhoa.DisplayMember = "khoadaotao";
            ckhoa.ValueMember = "MS_khoadaotao";
        }

        private void ckhoa_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select * from tb_Lopdaotao Where MS_Khoadaotao="+ckhoa.SelectedValue.ToString();
            DataTable dt2 = mLibrary.GetDataTable(msql, "table");
            clop.DataSource = dt2;
            clop.DisplayMember = "tenlop";
            clop.ValueMember = "MS_lop";
        }

        private void clop_SelectionChangeCommitted(object sender, EventArgs e)
        {          
            string mslop = clop.SelectedValue.ToString();
            string mskhoa = ckhoa.SelectedValue.ToString();
            msql = "Select a.ms_Mondaotao,a.mondaotao,a.ghichu " +
                   " from tb_Mondaotao a inner join tb_cauhinhlop b " +
                   " on a.MS_Mondaotao=b.MS_Mondaotao where B.ms_lop='" + mslop + "' Order by Mondaotao";
            DataTable dt1 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;


            msql = "Select a.* from TB_mondaotao a inner join tb_cauhinhkhoa b " +
                   " on a.MS_mondaotao=b.MS_mondaotao where b.ms_khoadaotao=" + mskhoa + " and a.MS_Mondaotao " +
                   " not in ( Select MS_Mondaotao from tb_Cauhinhlop where ms_Lop='" + mslop + "' ) " +
                   " Order by a.Mondaotao";
            DataTable dt2 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = "Select * from tb_Lopdaotao Where Ngaydangky<=getdate() " +
                        " and Getdate()<=Ngayketthuc and MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
            else
            {   
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = " Select * from tb_Lopdaotao Where MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
        }



        private void butup_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView2.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView2.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    DataGridViewRow row = dataGridView2.CurrentRow;
                    dataGridView2.Rows.Remove(row);
                    //DataTable dt2 = (DataTable)dataGridView2.DataSource;
                    //dt2.Rows[dataGridView2.CurrentRow.Index].Delete();


                    //dataGridView2.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butdown_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView2.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    //DataTable dt2 = (DataTable)dataGridView1.DataSource;
                    //dt2.Rows[dataGridView1.CurrentRow.Index].Delete();
                    //dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {
                    string mslop = clop.SelectedValue.ToString().Trim();
                    //Xóa thông tin củ
                    msql = "Delete TB_Cauhinhlop Where ms_lop='" + mslop + "'";
                    mLibrary.RunProc(msql);
                    //cập nhật thông tin mới
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count - 1; i++)
                        {
                            string msmon="";
                            try
                            {
                                msmon = dt.Rows[i]["MS_monDaoTao"].ToString();
                                msql = "  Insert into tb_cauhinhlop (MS_Lop,MS_Mondaotao,stamptime) Values('" + mslop + "'," + msmon + ",getdate())";
                                mLibrary.RunProc(msql);   
                            }
                            catch (Exception)
                            {
                                string a = "";
                            }
                                                   
                        }
                    }                    
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butLuu_Click_1(object sender, EventArgs e)
        {
            butLuu_Click(null, null);
        }          
     
    }
}
